package it.trekker.activities;

import it.trekker.R;
import it.trekker.db.ExcursionCoordinateDAO;
import it.trekker.db.ExcursionCoordinateTableMeta;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CursorAdapter;
import android.widget.SimpleCursorAdapter;

public class CoordinatesListViewActivity extends ListActivity {

//	private SQLiteDatabase db = null;
	private Cursor data = null;
	private CursorAdapter dataSource = null;
	private ExcursionCoordinateDAO gpsCoordinateDAO = null;
	private View entryView = null;
//	private ProgressDialog progressDialog = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
//		progressDialog = ProgressDialog.show(this, "Working..." , "Loading GPS points");
//		runOnUiThread(new LoadDataThread());
		Bundle params = getIntent().getExtras();
		int excursionId = params.getInt("excursion_id");
		new AsyncLoading().execute(excursionId);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		
		gpsCoordinateDAO.close();
		data.close();
	}
	
	
//	private void loadData() {
//		gpsCoordinateDAO = new GPSCoordinateDAO(this);
//
//		gpsCoordinateDAO.open();
//
//		Bundle params = getIntent().getExtras();
//		int excursionId = params.getInt("excursion_id");
//		data = gpsCoordinateDAO.getAllCoordinatesByExcursionAsCursor(excursionId);
//	}
	
	
	private void updateViews() {
		
		if(data.getCount() == 0) {
			String[] messages = new String[1];
			ArrayAdapter<String> itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, messages);
			getListView().setAdapter(itemAdapter);
	        
	        messages[0] = getString(R.string.no_gpspoints);
	        itemAdapter.notifyDataSetChanged();
		}
		else {
			String[] from = {	ExcursionCoordinateTableMeta.KEY_LATITUDE, 
					ExcursionCoordinateTableMeta.KEY_LONGITUDE, 
					ExcursionCoordinateTableMeta.KEY_ALTITUDE,
					ExcursionCoordinateTableMeta.KEY_TIME };

			int[] to = {R.id.coord_lat, R.id.coord_long, R.id.coord_alt, R.id.coord_date};

			dataSource = new SimpleCursorAdapter(this, R.layout.coordinates_listview_row, data, from, to);

			setListAdapter(dataSource);
		}
	}
	
	
	
	//***********************************************************************//
	//************************* Loading data thread *************************//
	//***********************************************************************//
	
//	private static final int UPDATE_END = 0;
	
	
//	private Handler handler = new Handler() {
//		@Override
//		public void handleMessage(Message msg) {
//			switch(msg.what) {
//				case UPDATE_END:	
//					progressDialog.dismiss();
//					updateViews();
//					break;
//				default:
//					super.handleMessage(msg);
//					break;
//			}
//		};
//	};
	
	
//	private class LoadDataThread implements Runnable {
//		@Override
//		public void run() {
//			loadData();		
//			handler.sendEmptyMessage(UPDATE_END);
//		}
//	}
	
	
	
	
	private class AsyncLoading extends AsyncTask<Integer, Void, Boolean> {
		
		private ProgressDialog progressDialog2;
		
		@Override
		protected void onPreExecute() {
			progressDialog2 = ProgressDialog.show(CoordinatesListViewActivity.this, "Working..." , "Loading GPS points");
		}

		@Override
		protected Boolean doInBackground(Integer... params) {
			
//			publishProgress(null);
			gpsCoordinateDAO = new ExcursionCoordinateDAO(CoordinatesListViewActivity.this);

			gpsCoordinateDAO.open();

			data = gpsCoordinateDAO.getAllCoordinatesByExcursionAsCursor(params[0]);
			
			return true;
		}
		
		@Override
		protected void onPostExecute(Boolean result) {
			updateViews();
			progressDialog2.dismiss();
		}
		
//		@Override
//		protected void onProgressUpdate(Integer... values) {
//			progressDialog2 = ProgressDialog.show(CoordinatesListViewActivity.this, "Working..." , "Loading GPS points");
//		}
		
	}
}
